package com.lyb.service.impl;

import com.lyb.mapper.UserMapper;
import com.lyb.pojo.User;
import com.lyb.service.UserService;
import com.lyb.utils.Result;
import com.lyb.utils.ResultCodeEnum;
import org.apache.ibatis.ognl.OgnlContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public Result login(User user) {
        User newUser = userMapper.login(user.getUsername());
      if (newUser.getUsername() == null){
          return Result.fail("用户名不能为空");
      }

      if (!user.getPassword().equals(newUser.getPassword())){
          return Result.fail("密码错误");
      }

      Map map = new HashMap();
      map.put("uid",newUser.getUId());
      return Result.ok(map);
    }

    @Override
    public Result register(User user) {
        user.setCreateTime(new Date());
        user.setUpdateTime(new Date());
        userMapper.register(user);
        return Result.ok();
    }

    @Override
    public Result updateUser(User user) {
        user.setUpdateTime(new Date());
        userMapper.updateUser(user);
        return Result.ok();
    }

    @Override
    public Result getUser(Integer id) {
        User user = userMapper.getUser(id);
        user.setPassword("");
        return Result.build(user,ResultCodeEnum.SUCCESS);
    }
}
